package com.cin.cma.client.service.impl;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cin.cma.client.config.OpenClient;
import com.cin.cma.client.config.OpenConfig;
import com.cin.cma.client.model.CloudClient;
import com.cin.cma.client.model.DataCinema;
import com.cin.cma.client.repository.dao.GvIncrementBookingTypeDO;
import com.cin.cma.client.repository.dao.GvLocationsDO;
import com.cin.cma.client.repository.dao.GvUsersDO;
import com.cin.cma.client.repository.dao.GvWorkStationsDO;
import com.cin.cma.client.repository.mapper.GvIncrementBookingTypeMapper;
import com.cin.cma.client.repository.mapper.GvLocationsMapper;
import com.cin.cma.client.repository.mapper.GvUsersMapper;
import com.cin.cma.client.repository.mapper.GvWorkStationsMapper;
import com.cin.cma.client.service.IConnectionService;
import com.cin.cma.client.service.IFinixxProxyService;
import com.cin.cma.client.service.IMaoyanService;
import com.cin.cma.common.constant.EventConstant;
import com.cin.cma.common.model.ApiResponse;
import io.socket.client.IO;
import io.socket.client.Socket;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ConnectionServiceImpl implements IConnectionService {

    private static DataCinema result = null;
    @Value("${socketio.port}")
    private int port;
    @Value("${socketio.host}")
    private String host;
    @Autowired
    private OpenConfig openConfig;
    @Autowired
    private IMaoyanService maoyanService;
    @Autowired
    private IFinixxProxyService httpService;
    @Resource
    private GvLocationsMapper gvLocationsMapper;
    @Resource
    private GvIncrementBookingTypeMapper gvIncrementBookingTypeMapper;
    @Resource
    private GvUsersMapper gvUsersMapper;
    @Resource
    private GvWorkStationsMapper gvWorkStationsMapper;

    private static @NotNull GvWorkStationsDO getGvWorkStationsDO(String flag) {
        GvWorkStationsDO gvWorkStationsDO = new GvWorkStationsDO();
        gvWorkStationsDO.setWsnWorkstationId(flag);
        gvWorkStationsDO.setWsnWorkstationDesc(flag);
        gvWorkStationsDO.setWsnLocationCd(result.getCinemaLinkId());
        gvWorkStationsDO.setWsnRcreUserId("ADMIN");
        gvWorkStationsDO.setWsnRcreDate(new Date());
        gvWorkStationsDO.setWsnLchgUserId(null);
        gvWorkStationsDO.setWsnLchgDate(null);
        gvWorkStationsDO.setWsnLayoutId(null);
        gvWorkStationsDO.setWsnKindCd("2");
        gvWorkStationsDO.setWsnOtherWorkstations(null);
        gvWorkStationsDO.setWsnPosFlg("Y");
        gvWorkStationsDO.setWsnGaFlg("Y");
        gvWorkStationsDO.setWsnPrintFlg("Y");
        gvWorkStationsDO.setWsnStokingFlg("N");
        return gvWorkStationsDO;
    }

    private static @NotNull GvUsersDO getGvUserDO(String flag) {
        GvUsersDO gvUsersDO = new GvUsersDO();
        gvUsersDO.setUsrUserId(flag);
        gvUsersDO.setUsrUserPassword("0F4FB5985B9828787954060C271D72D2");
        gvUsersDO.setUsrLastName("微信");
        gvUsersDO.setUsrFirstName("订购");
        gvUsersDO.setUsrJoinDt(new Date());
        gvUsersDO.setUsrRoleCd("KIOSK");
        gvUsersDO.setUsrLocationCd(result.getCinemaLinkId());
        gvUsersDO.setUsrPasscardNo(null);
        gvUsersDO.setUsrPasscardIssueDt(DateTime.of("0001-01-01", "yyyy-MM-dd"));
        gvUsersDO.setUsrPasscardExpiryDt(DateTime.of("0001-01-01", "yyyy-MM-dd"));
        gvUsersDO.setUsrLoginFlg("N");
        gvUsersDO.setUsrConcurrencyFlg("N");
        gvUsersDO.setUsrRcreUserId("ADMIN");
        gvUsersDO.setUsrRcreDate(new Date());
        gvUsersDO.setUsrLchgUserId("ADMIN");
        gvUsersDO.setUsrLchgDate(new Date());
        gvUsersDO.setUsrPasscardFlg("N");
        gvUsersDO.setUsrPosUsedRange(null);
        gvUsersDO.setUsrIsAddGoodSingle("N");
        gvUsersDO.setUsrIsAddGoodMaterial("N");
        gvUsersDO.setUsrIsAddGoodCompound("N");
        gvUsersDO.setUsrIsAddGoodCombo("N");
        gvUsersDO.setUsrUserPwValidity(DateTime.of("4712-12-31", "yyyy-MM-dd"));
        gvUsersDO.setUsrUserPwValidityDays(null);
        gvUsersDO.setUsrUserNo(null);
        gvUsersDO.setUsrExternalPassword(null);
        gvUsersDO.setUsrDepartmentCd(null);
        gvUsersDO.setUsrSchedulingFlg("N");
        gvUsersDO.setGsuUserTypeCd(null);
        gvUsersDO.setUsrUserTypeCd(null);
        gvUsersDO.setUsrPhone(null);
        gvUsersDO.setUsrEmail(null);
        gvUsersDO.setUsrSchedulingPlace(null);
        gvUsersDO.setUsrFirstLoginUpdate("Y");
        gvUsersDO.setUsrCloudFlg(null);
        return gvUsersDO;
    }

    private static @NotNull GvIncrementBookingTypeDO getGvIncrementBookingTypeDO(String flag) {
        GvIncrementBookingTypeDO gvIncrementBookingType = new GvIncrementBookingTypeDO();
        gvIncrementBookingType.setIbtBookingTypeCd(flag);
        gvIncrementBookingType.setIbtBookingTypeDesc("微信订购");
        gvIncrementBookingType.setIbtRcreUserCd(flag);
        gvIncrementBookingType.setIbtAvailFlg("Y");
        gvIncrementBookingType.setIbtAvailChannels(268436699);
        gvIncrementBookingType.setIbtVipFlg("N");
        gvIncrementBookingType.setIbtDelaySaleOffsetTime(-10);
        gvIncrementBookingType.setIbtBookingBefShowTime(45);
        gvIncrementBookingType.setIbtBookingDays(3);
        gvIncrementBookingType.setIbtMaxTktsPer(6);
        gvIncrementBookingType.setIbtBookingFee(0);
        gvIncrementBookingType.setIbtBookingFeeApplInd("T");
        gvIncrementBookingType.setIbtVisibleFlg("Y");
        gvIncrementBookingType.setIbtRcreUserId(flag);
        gvIncrementBookingType.setIbtRcreDate(new Date());
        gvIncrementBookingType.setIbtLchgUserId(flag);
        gvIncrementBookingType.setIbtLchgDate(new Date());
        gvIncrementBookingType.setIbtWebSaleFlg("N");
        gvIncrementBookingType.setIbtTerminalPass(null);
        gvIncrementBookingType.setIbtTerminalcount(5);
        gvIncrementBookingType.setIbtAllowDeleteFlg("Y");
        gvIncrementBookingType.setIbtAuthorizationNo("0");
        gvIncrementBookingType.setIbtModulus("0");
        gvIncrementBookingType.setIbtExponent("0");
        gvIncrementBookingType.setIbtAuthorizationFlg("N");
        gvIncrementBookingType.setIbtThirdParty("Y");
        gvIncrementBookingType.setIbtPosEnable("Y");
        return gvIncrementBookingType;
    }

    /**
     * 获取影院信息
     *
     * @return DataCinema
     */
    @Override
    public DataCinema getLocalCinema() {
        //查询本地影院基础
        if (result == null) {
            result = new DataCinema();
            for (int i = 0; i < 1000; i++) {
                try {
                    List<GvLocationsDO> results = this.gvLocationsMapper.selectList(Wrappers.lambdaQuery(GvLocationsDO.class)
                            //影院内码、八位编码、影院名称
                            .select(GvLocationsDO::getLocLocationCd, GvLocationsDO::getLocCode, GvLocationsDO::getLocName)
                            .eq(GvLocationsDO::getLocLocalFlg, "Y"));
                    if (results.isEmpty()) {
                        throw new Exception("db exception,not gv_location result");
                    }

                    result.setCinemaLinkId(results.get(0).getLocLocationCd());
                    result.setCinemaCode(results.get(0).getLocCode());
                    result.setCinemaName(results.get(0).getLocName());
                    break;
                } catch (Exception e) {
                    log.error("查询数据库异常（我将尝试1000次），目前第{}次", i, e);
                    ThreadUtil.sleep(1000);
                }
            }
        }

        initGvDatabase("WEIXIN");
        ThreadUtil.execAsync(() -> {
            for (int i = 0; i < 5; i++) {
                //重启缓存服务
                try {
                    // 停止服务
                    Runtime.getRuntime().exec("sc stop Memcached Server").waitFor();
                    // 终止相关进程
                    Runtime.getRuntime().exec("taskkill /F /IM memcached.exe").waitFor();

                    Thread.sleep(5000);

                    Runtime.getRuntime().exec("sc start Memcached Server").waitFor();
                    break;
                } catch (IOException | InterruptedException e) {
                    log.error("重启缓存服务异常", e);
                }
            }
        });
        //检查程序是否授权
        try {
            InetAddress address = InetAddress.getLocalHost();
            result.setComputer(address.getHostName());
            result.setIpv4s(StrUtil.join(",", NetUtil.localIpv4s().stream().filter(v -> !"127.0.0.1".equals(v)).collect(Collectors.toList())));
        } catch (Exception e) {
            log.error("获取机器名或ipv4异常", e);
        }
        return result;
    }

    private void initGvDatabase(String flag) {
        long count = this.gvIncrementBookingTypeMapper.selectCount(Wrappers.lambdaQuery(GvIncrementBookingTypeDO.class)
                .eq(GvIncrementBookingTypeDO::getIbtBookingTypeCd, flag));
        if (count <= 0) {
            GvIncrementBookingTypeDO gvIncrementBookingType = getGvIncrementBookingTypeDO(flag);
            this.gvIncrementBookingTypeMapper.insert(gvIncrementBookingType);
        }
        count = this.gvUsersMapper.selectCount(Wrappers.lambdaQuery(GvUsersDO.class).eq(GvUsersDO::getUsrUserId, flag));
        if (count <= 0) {
            GvUsersDO gvUsersDO = getGvUserDO(flag);
            this.gvUsersMapper.insert(gvUsersDO);
        }
        count = this.gvWorkStationsMapper.selectCount(Wrappers.lambdaQuery(GvWorkStationsDO.class).eq(GvWorkStationsDO::getWsnWorkstationId, flag));
        if (count <= 0) {
            GvWorkStationsDO gvWorkStationsDO = getGvWorkStationsDO(flag);
            this.gvWorkStationsMapper.insert(gvWorkStationsDO);
        }
    }

    /**
     * 检查是否授权
     *
     * @param dataCinema 影院信息
     */
    @Override
    public void checkAuth(DataCinema dataCinema) {
        int today = Convert.toInt(DateTime.now().toString("yyyyMMdd"));
        while (true) {
            try {
                ApiResponse<?> apiResponse = OpenClient.builder().config(openConfig).build().execute("/cinema-api/client/check", dataCinema);
                if (!apiResponse.getSuccess()) {
                    throw new Exception(apiResponse.getError().getCode() + "::" + apiResponse.getError().getMessage());
                }
                CloudClient cloudClient = ((JSONObject) apiResponse.getData()).toJavaObject(CloudClient.class);
                log.info("检查客户端是否授权-通过1");
                if (!cloudClient.getAuthFlag()) {
                    throw new Exception("系统未授权！");
                }
                log.info("检查客户端是否授权-通过2");
                int expireDate = Convert.toInt(DateTime.of(cloudClient.getExpireDate()).toString("yyyyMMdd"));
                if (today > expireDate) {
                    throw new Exception("系统已过期！");
                }
                log.info("检查客户端是否授权-通过3");
                break;
            } catch (Exception e) {
                log.error("等待系统授权！[ps:不授权我不会进行下一步,并每5秒探针一次！]");
                ThreadUtil.sleep(5000);
            }
        }
    }

    /**
     * Socket连接
     *
     * @param cinemaLinkId 影院内码
     */
    @Override
    public void onConnectionSocket(String cinemaLinkId) {
        // 服务端socket.io连接通信地址
        String url = StrUtil.format("ws://{}:{}?cinemaLinkId={}", host, port, cinemaLinkId);
        try {
            IO.Options options = new IO.Options();
            options.transports = new String[]{"websocket"};
            //失败后重试次数
            options.reconnectionAttempts = 999999;
            // 失败重连的时间间隔
            options.reconnectionDelay = 2000;
            // 连接超时时间(ms)
            options.timeout = 5000;

            final Socket socket = IO.socket(url, options);
            log.info("Socket连接准备:{}", url);
            // 定时检查连接状态（每 10 秒）
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    if (!socket.connected()) {
                        System.out.println("检测到连接断开，正在重连...");
                        socket.connect();
                    }
                }
            }, 0, 10000);
            // 延迟 0ms，间隔 10s
            // 监听连接成功（直接使用字符串 "connect"）
            socket.on("connect", args1 -> {
                System.out.println("[Socket.IO] 连接成功！");
            });

            // 监听连接断开（直接使用字符串 "disconnect"）
            socket.on("disconnect", reason -> {
                System.out.println("[Socket.IO] 连接断开，原因: " + Arrays.toString(reason));
            });

            // 监听连接错误（直接使用字符串 "connect_error"）
            socket.on("connect_error", error -> {
                System.out.println("[Socket.IO] 连接错误: " + Arrays.toString(error));
            });

            // 监听重连尝试（直接使用字符串 "reconnect_attempt"）
            socket.on("reconnect_attempt", attempt -> {
                System.out.println("[Socket.IO] 尝试第 " + (socket.io().reconnectionAttempts() + 1) + " 次重连...");
            });

            // 监听重连成功（直接使用字符串 "reconnect"）
            socket.on("reconnect", args1 -> {
                System.out.println("[Socket.IO] 重连成功！");
            });

            // 监听重连失败（直接使用字符串 "reconnect_failed"）
            socket.on("reconnect_failed", args1 -> {
                System.out.println("[Socket.IO] 重连失败，放弃重连！");
            });
            // 自定义事件`event` -> 接收服务端成功连接消息
            socket.on(EventConstant.Other.OTHER_CUSTOM, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Other.OTHER_CUSTOM, this.httpService.request(objects[0].toString()));
            }));
            socket.on(EventConstant.Basic.BASIC_GET_PASS_NUMBER, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Basic.BASIC_GET_PASS_NUMBER, this.httpService.request("api/basic/getpassnum", objects[0].toString()));
            }));
            socket.on(EventConstant.Basic.BASIC_GET_LOCAL_CINEMA, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Basic.BASIC_GET_LOCAL_CINEMA, this.httpService.request("api/basic/getlocalcinema", objects[0].toString()));
            }));
            socket.on(EventConstant.Basic.BASIC_BOOKING_ORDER, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Basic.BASIC_BOOKING_ORDER, this.httpService.request("api/basic/bookingOrder", objects[0].toString()));
            }));
            socket.on(EventConstant.Basic.BASIC_GLOBAL_SYSTEM_TIME, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Basic.BASIC_GLOBAL_SYSTEM_TIME, this.httpService.request("api/basic/globalsystemtime", objects[0].toString()));
            }));
            socket.on(EventConstant.Basic.BASIC_GET_INCREASE_BOOKING_SETTING, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Basic.BASIC_GET_INCREASE_BOOKING_SETTING, this.httpService.request("api/basic/getincreasebookingsetting", objects[0].toString()));
            }));
            socket.on(EventConstant.Basic.BASIC_ALL_SYSTEM_SETTING, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Basic.BASIC_ALL_SYSTEM_SETTING, this.httpService.request("api/basic/allsystemsetting", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_CONSUME, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_CONSUME, this.httpService.request("api/card/cardconsume", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_GET_CARD_INFO, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_GET_CARD_INFO, this.httpService.request("api/card/cardinfo", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_CONSUME_ROLLBACK, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_CONSUME_ROLLBACK, this.httpService.request("api/card/cardconsumerollback", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_SUMMARY_INFO, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_SUMMARY_INFO, this.httpService.request("api/card/getsummaryinfo", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_ADD_MONEY, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_ADD_MONEY, this.httpService.request("api/card/cardaddmoney", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_BUYING, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_BUYING, this.httpService.request("api/card/cardbuying", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_GET_ADD_MONEY, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_GET_ADD_MONEY, this.httpService.request("api/card/queryaddmoney", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_GET_MARKING, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_GET_MARKING, this.httpService.request("api/card/querymarking", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_RESET_PWD, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_RESET_PWD, this.httpService.request("api/card/cardresetpassword", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_LOCK, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_LOCK, this.httpService.request("api/card/cardlock", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_ACTIVATION, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_ACTIVATION, this.httpService.request("api/card/cardactivation", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_EDIT_MEMBER_INFO, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_EDIT_MEMBER_INFO, this.httpService.request("api/card/cardeditmemberinfo", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_SELL, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_SELL, this.httpService.request("api/card/cardsell", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_SELL_RECHARGE, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_SELL_RECHARGE, this.httpService.request("api/card/cardsellrecharge", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_SELL_INFO, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_SELL_INFO, this.httpService.request("api/card/getcardsellinfo", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_GET_USE_POLICY_LIST, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_GET_USE_POLICY_LIST, this.httpService.request("api/card/cardprefabricateusepolicy", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_USE_POLICY, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_USE_POLICY, this.httpService.request("api/card/usepolicy", objects[0].toString()));
            }));
            socket.on(EventConstant.Card.CARD_GET_GRADE_LIST, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Card.CARD_GET_GRADE_LIST, this.httpService.request("api/card/cardprefabricategrade", objects[0].toString()));
            }));
            socket.on(EventConstant.DataInf.DATAINF_GET_SOFTWARE_VERSION, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.DataInf.DATAINF_GET_SOFTWARE_VERSION, this.httpService.request("api/datainf/getsoftwareversion", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_GET_SESSION, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_GET_SESSION, this.httpService.request("api/ticket/session", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_GET_SESSION_FILMS, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_GET_SESSION_FILMS, this.httpService.request("api/ticket/sessionfilm", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_GET_HALLS, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_GET_HALLS, this.httpService.request("api/ticket/halls", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_GET_SECTIONS, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_GET_SECTIONS, this.httpService.request("api/ticket/sections", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_GET_SEAT_PLAN, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_GET_SEAT_PLAN, this.httpService.request("api/ticket/seatplan", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_GET_SESSION_PRICE_POLICY, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_GET_SESSION_PRICE_POLICY, this.httpService.request("api/ticket/sessionpricepolicy", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_GET_BOOKING_ID, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_GET_BOOKING_ID, this.httpService.request("api/ticket/getbookingid", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_REPOSITION_SEATS, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_REPOSITION_SEATS, this.httpService.request("api/ticket/repositionseats", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_RELEASE_SEATS, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_RELEASE_SEATS, this.httpService.request("api/ticket/releaseseats", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_GET_ALLOCATE_SEATS, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_GET_ALLOCATE_SEATS, this.httpService.request("api/ticket/getallocateseats", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_GET_CONFIRM_ID, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_GET_CONFIRM_ID, this.httpService.request("api/ticket/getconfirmid", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_QUERY_ORDER, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_QUERY_ORDER, this.httpService.request("api/ticket/ticketqueryorder", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_GET_BOOKING_BILL, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_GET_BOOKING_BILL, this.httpService.request("api/ticket/getbookingbill", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_GET_TICKETS, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_GET_TICKETS, this.httpService.request("api/ticket/gettickets", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_REFUND, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_REFUND, this.httpService.request("api/ticket/refundtickets", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_CARD_PRICE_POLICY, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_CARD_PRICE_POLICY, this.httpService.request("api/ticket/cardpricepolicy", objects[0].toString()));
            }));
            socket.on(EventConstant.Ticket.TICKET_FILM_IMAGE, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Ticket.TICKET_FILM_IMAGE, this.httpService.request("api/ticket/filmimage", objects[0].toString()));
            }));
            socket.on(EventConstant.Voucher.VOUCHER_REFUND, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Voucher.VOUCHER_REFUND, this.httpService.request("api/voucher/voucherrefund", objects[0].toString()));
            }));
            socket.on(EventConstant.Voucher.VOUCHER_GET, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Voucher.VOUCHER_GET, this.httpService.request("api/voucher/getvoucher", objects[0].toString()));
            }));
            socket.on(EventConstant.Voucher.VOUCHER_TEMP_LOCK, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Voucher.VOUCHER_TEMP_LOCK, this.httpService.request("api/voucher/vouchertemplock", objects[0].toString()));
            }));
            socket.on(EventConstant.Voucher.VOUCHER_REDEEM, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Voucher.VOUCHER_REDEEM, this.httpService.request("api/voucher/voucherredeem", objects[0].toString()));
            }));
            socket.on(EventConstant.Voucher.VOUCHER_REDEEM_ROLLBACK, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Voucher.VOUCHER_REDEEM_ROLLBACK, this.httpService.request("api/voucher/voucherredeemrollback", objects[0].toString()));
            }));
            socket.on(EventConstant.Voucher.VOUCHER_GET_PAY_POLICY, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Voucher.VOUCHER_GET_PAY_POLICY, this.httpService.request("api/voucher/getvoucherpaypolicy", objects[0].toString()));
            }));

            socket.on(EventConstant.Goods.GOODS_GET_GOODS_CLASS, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Goods.GOODS_GET_GOODS_CLASS, this.httpService.request("api/shop/PosGoodsBigKind", objects[0].toString()));
            }));
            socket.on(EventConstant.Goods.GOODS_GET_GOODS_HOT, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Goods.GOODS_GET_GOODS_HOT, this.httpService.request("api/shop/Pos_select_HotSale_Dtls", objects[0].toString()));
            }));
            socket.on(EventConstant.Goods.GOODS_GET_PACKAGE_DETAILS, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Goods.GOODS_GET_PACKAGE_DETAILS, this.httpService.request("api/shop/Pos_FFormula_Details", objects[0].toString()));
            }));
            socket.on(EventConstant.Goods.GOODS_GET_GOODS_PACKAGE, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Goods.GOODS_GET_GOODS_PACKAGE, this.httpService.request("api/shop/Pos_FFormula", objects[0].toString()));
            }));
            socket.on(EventConstant.Goods.GOODS_GET_GOODS_LIST, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Goods.GOODS_GET_GOODS_LIST, this.httpService.request("api/shop/PosGoodsListClassPlace", objects[0].toString()));
            }));
            socket.on(EventConstant.Goods.GOODS_GET_ORDER_INFO, objects -> ThreadUtil.execAsync(() -> {
                socket.emit(EventConstant.Goods.GOODS_GET_ORDER_INFO, this.httpService.request("api/shop/pos_select_order_info", objects[0].toString()));
            }));
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
